package com.sanalkitaplik.resourcemanagement;

import java.util.ArrayList;
import java.util.List;

import javax.faces.event.ActionEvent;

public class ResourceBrowsingManagerBean {
	
	private List<BrowsedBook> books;
	private int rows;
	private int page;

	public ResourceBrowsingManagerBean() {
		this.books = new ArrayList<BrowsedBook>();
		page = 1;
		rows = 5;
		for (int i=1; i<50; i++) {
			BrowsedBook book = new BrowsedBook();
			book.setTitle("Kitap " + i);
			book.addAuthor("Yazar " + i);
			book.setCover("/images/book.png");
			book.setOwner("Kullanıcı" + i);
			book.setDescription("Burada " + i + ". kitabın açıklaması olacak.");
			book.setGenre("Tür " + i);
			books.add(book);
		}
	}

	public List<BrowsedBook> getBooks() {
		List<BrowsedBook> currentBooks = new ArrayList<BrowsedBook>();
		int first = (page-1)*rows;
		int last = first+rows;
		for (int i = first; i < last; i++)
			currentBooks.add(books.get(i));
		return currentBooks;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public int getRows() {
		return rows;
	}
	
	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}
	
	public void next(ActionEvent event) {
		if (!getLastPage())
			page++;
	}
	
	public void back(ActionEvent event) {
		if (!getFirstPage())
			page--;
	}
	
	public boolean getLastPage() {
		return page == (books.size()+rows-1)/rows;
	}
	
	public boolean getFirstPage() {
		return page == 1;
	}

}
